package com.example.smart_campus.service.impl;


import com.baomidou.mybatisplus.core.batch.MybatisBatch;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.smart_campus.common.Res;
import com.example.smart_campus.pojo.dto.students.StudentQuery;
import com.example.smart_campus.pojo.dto.students.StudentUpdate;
import com.example.smart_campus.pojo.dto.students.StudentsTable;
import com.example.smart_campus.pojo.entity.Students;
import com.example.smart_campus.dao.StudentsMapper;
import com.example.smart_campus.service.StudentsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.github.timoyung.mybatisplusbatch.service.BatchServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;


/**
 * <p>
 * 学生表，存储学生的基本信息 服务实现类
 * </p>
 *
 * @author liyv
 * @since 2024-10-19
 */
@Service
public class StudentsServiceImpl extends ServiceImpl<StudentsMapper, Students> implements StudentsService {
    @Autowired
    private StudentsMapper studentsMapper;

    @Override
    public Res importByEList(List<StudentsTable> tableList) {
        Students s = new Students(tableList.get(0));
        List<Students> StudentList = new ArrayList<>();
        tableList.forEach(item->{
            Students student = new Students(item);
            StudentList.add(student);
        });
        int size = StudentList.size();

       if(studentsMapper.insertBatchSomeColumn(StudentList) == size) {
           return Res.ok() ;
       }

        return Res.failed() ;
    }

    @Override
    public Res<List<Students>> getAllStudents() {
        return Res.ok(studentsMapper.selectList(Wrappers.emptyWrapper()));
    }

    @Override
    public Res<List<Students>> getByQuery(StudentQuery query) {
        QueryWrapper<Students> queryWrapper = new QueryWrapper<>() ;
        queryWrapper.like(!query.getSearch().isEmpty(),"name",query.getSearch())
                .or().like(!query.getSearch().isEmpty(),"student_no",query.getSearch());

        return Res.ok(studentsMapper.selectList(queryWrapper));
    }

    @Override
    @Transactional
    public Res delById(String sid) {
        int i = studentsMapper.deleteById(sid);
        if (i > 0)
            return Res.ok();
        return Res.failed();
    }

    @Override
    @Transactional
    public Res updateStudent(StudentUpdate update) {
        Students s = studentsMapper.selectById(update.getStudentId()) ;
        if (s == null)
            return Res.failed("not found");
        UpdateWrapper<Students> wrapper = new UpdateWrapper<>();
        wrapper.eq("student_id",update.getStudentId())
                .set(!update.getStudentNo().isEmpty(),"student_no",update.getStudentNo())
                .set(!update.getName().isEmpty(),"name",update.getName())
                .set(!update.getMajor().isEmpty(),"major",update.getMajor())
                .set(!update.getGrade().isEmpty(),"grade",update.getGrade()) ;
        int r = studentsMapper.update(s,wrapper);
        if (r > 0) return Res.ok() ;
        return Res.failed();
    }
}
